﻿using System.Windows;
using System.Windows.Input;
using LFSRecord2.Events;
using LFSRecord2.Model;
using LFSRecord2.Model.UserActions;
using LFSRecord2.ViewModel;

namespace LFSRecord2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ConnectionInfo_MouseDown(object sender, MouseButtonEventArgs e)
        {
            MainWindowViewModel.InSimConnectCommandExecute(((LfsRecordController.insim != null && LfsRecordController.insim.IsConnected) ? "0" : "1"));
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (!IsKeyboardFocused)
                return;
            //Debug.WriteLine("Key : {0}", e.Key);

            switch (e.Key)
            {
                case Key.X:
                    if (Keyboard.Modifiers == ModifierKeys.Control)
                    {
                        // Cut (keyframes)
                        LfsRecordController.project.CutSelectedKeyFrames();
                        LfsRecordController.CreateUndoLevel();
                    }
                    break;

                case Key.C:
                    if (Keyboard.Modifiers == ModifierKeys.Control)
                    {
                        // Copy (keyframes)
                        LfsRecordController.project.CopySelectedKeyFrames();
                        LfsRecordController.CreateUndoLevel();
                    }
                    else
                    {
                        LfsRecordController.GetCppFromLfs();
                    }
                    break;

                case Key.V:
                    if (Keyboard.Modifiers == ModifierKeys.Control)
                    {
                        // Paste (keyframes)
                        LfsRecordController.project.PasteSelectedKeyFrames();
                        LfsRecordController.CreateUndoLevel();
                    }
                    break;

                case Key.N:
                    if (Keyboard.Modifiers == ModifierKeys.Control)
                    {
                        // New project
                        ((MainWindowViewModel)DataContext).NewCommandExecute();
                    }
                    break;

                case Key.O:
                    if (Keyboard.Modifiers == ModifierKeys.Control)
                    {
                        // Load file
                        ((MainWindowViewModel)DataContext).LoadCommandExecute();
                    }
                    break;

                case Key.S:
                    if (Keyboard.Modifiers == ModifierKeys.Control)
                    {
                        // Save file
                        ((MainWindowViewModel)DataContext).SaveCommandExecute();
                    }
                    break;

                //case Key.U:
                //    LFSRecordController.SetShiftU(true);
                //    break;

                case Key.Y:
                    if (Keyboard.Modifiers == ModifierKeys.Control)
                    {
                        LfsRecordController.ApplyRedo();
                    }
                    break;

                case Key.Z:
                    if (Keyboard.Modifiers == ModifierKeys.Control)
                    {
                        LfsRecordController.ApplyUndo();
                    }
                    break;
                
                case Key.Space:
                case Key.Return:
                    // Start / Stop playback
                    if (LfsRecordController.PlaybackState == PlaybackStates.Stopped)
                    {
                        LfsRecordController.StartPlayback();
                    }
                    else
                    {
                        LfsRecordController.StopPlayback();
                    }

                    break;

                case Key.NumPad0:
                    LfsRecordController.StopPlayback();
                    break;

                case Key.Multiply:
                    if (LfsRecordController.PlaybackState == PlaybackStates.Recording || LfsRecordController.PlaybackState == PlaybackStates.RecordingReplay)
                        LfsRecordController.StopPlayback();
                    else
                        LfsRecordController.StartRecord();
                    break;

                case Key.Delete:
                    // Record the delete user action(s) before deleting the keyframes
                    LfsRecordController.project.SubmitUserActionDeleteSelectedKeyFrames();
                    LfsRecordController.project.SubmitUserActionDeleteSelectedAudioRegions();

                    // Delete the keyframes
                    LfsRecordController.project.DeleteSelectedKeyFrames();
                    LfsRecordController.project.DeleteSelectedAudioRegions();
                    LfsRecordController.CreateUndoLevel();
                    break;

                case Key.PageUp:
                    if (LfsRecordController.FrameIndex > 0)
                        ServicesFactory.EventService.GetEvent<FrameIndexChangedEvent>().Publish(new FrameIndexChangedEventArgs { Sender = this, NewFrameIndex = LfsRecordController.FrameIndex - 1, UpdateCurrentValues = true, UpdateReplayPosition = true });
                    break;

                case Key.PageDown:
                    if (LfsRecordController.FrameIndex < (int)(LfsRecordController.project.ProjectLength * LfsRecordController.project.FPS))
                        ServicesFactory.EventService.GetEvent<FrameIndexChangedEvent>().Publish(new FrameIndexChangedEventArgs { Sender = this, NewFrameIndex = LfsRecordController.FrameIndex + 1, UpdateCurrentValues = true, UpdateReplayPosition = true });
                    break;

                case Key.Home:
                    ServicesFactory.EventService.GetEvent<FrameIndexChangedEvent>().Publish(new FrameIndexChangedEventArgs { Sender = this, NewFrameIndex = 0, UpdateCurrentValues = true, UpdateReplayPosition = true });
                    break;

                case Key.End:
                    ServicesFactory.EventService.GetEvent<FrameIndexChangedEvent>().Publish(new FrameIndexChangedEventArgs { Sender = this, NewFrameIndex = (int)(LfsRecordController.project.ProjectLength * LfsRecordController.project.FPS), UpdateCurrentValues = true, UpdateReplayPosition = true });
                    break;
            }
        }
    }
}
